package com.wei.ssm.controller;

import com.wei.ssm.domain.Permission;
import com.wei.ssm.domain.Role;
import com.wei.ssm.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.jws.WebParam;
import java.util.List;
@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    @RequestMapping("/addPermissionToRole.do")
    public String addPermissionToRole(@RequestParam(name = "roleId") int roleId,@RequestParam(name = "ids") int[] ids){
        roleService.addPermissionToRole(roleId,ids);
        return "redirect:/role/findAll.do";
    }


    @RequestMapping("/roleAddPermission.do")
    public ModelAndView roleAddPermission(@RequestParam(name = "id") int id) throws Exception {
        ModelAndView modelAndView = new ModelAndView();
        //找到这个角色
        Role role = roleService.findById(id);
        //找到其他的权限
        List<Permission> permissionList = roleService.findOtherPermiss(id);

        modelAndView.addObject("role",role);
        modelAndView.addObject("permissionList",permissionList);
        modelAndView.setViewName("role-permission-add");
        return modelAndView;

    }

    @RequestMapping("/deleteById.do")
    public String deleteById(@RequestParam(name = "id",required = true) int id) throws Exception {
        roleService.deleteById(id);
        return "redirect:/role/findAll.do";
    }



    @RequestMapping("/findById.do")
    public ModelAndView findById(@RequestParam(name = "id",required = true) int id) throws Exception {
        ModelAndView modelAndView = new ModelAndView();
        Role role = roleService.findById(id);
        modelAndView.addObject("role",role);
        modelAndView.setViewName("role-show");
        return modelAndView;
    }



    @RequestMapping("/save.do")
    public String save(Role role){
        roleService.save(role);
        return "redirect:/role/findAll.do";
    }

    @RequestMapping("/findAll.do")
    public ModelAndView findAll(){
        ModelAndView modelAndView = new ModelAndView();
        List<Role> roleList =roleService.findAll();
        modelAndView.addObject("roleList",roleList);
        modelAndView.setViewName("role-list");
        return modelAndView;

    }

}
